package com.altio.swing.portlet;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;

import javax.swing.JComponent;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;

import com.altio.swing.portlet.PortletPanelButton.ActionOperation;

public class PortletPanelButtonUI extends BasicButtonUI {
    private int lineWidth = 4;
    
    private static PortletPanelButtonUI instance = null;
    
    public static ComponentUI createUI(JComponent c) {
      if(instance == null) {
        instance = new PortletPanelButtonUI();
      }
      
      return instance;
    }

    @Override
    public void installUI(JComponent c) {
      super.installUI(c);
      c.setOpaque(false);
      c.setBorder(new EmptyBorder(0, 0, 0, 0));
    }
    
    @Override
    public void paint(Graphics g, JComponent c) {
      Graphics2D g2d = (Graphics2D)g.create();
      
      ActionOperation action = ActionOperation.CLOSE;
      if(c instanceof PortletPanelButton) {
        action = ((PortletPanelButton) c).getActionOperation();
        
      }

      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);
      
      int iconWidth = (int) (Math.min(c.getWidth(), c.getHeight()) * 0.75);
      double rotation = Math.PI / 4;
      if(action == ActionOperation.RESTORE) {
        rotation *= 5;

        g2d.translate(0, -(iconWidth / 4));
      } else if(action == ActionOperation.MINIMIZE) {
        g2d.translate(0, iconWidth / 8);        
      }
      
//      if (action != ActionOperation.CLOSE){
//        g2d.translate(0, iconWidth / 4);        
//      }
      
      g2d.rotate(rotation, c.getWidth() / 2, c.getHeight() / 2);
      
      
      if(((PortletPanelButton) c).getModel().isRollover()) {
        g2d.setColor(c.getBackground().darker());
        drawIcon(c, g2d, action, lineWidth + 2);
      }

      g2d.setColor(Color.white);
      drawIcon(c, g2d, action, lineWidth);

      if(!((PortletPanelButton) c).getModel().isPressed()) {
        g2d.setColor(c.getBackground().darker());
        drawIcon(c, g2d, action, lineWidth - 2);
      }

      g2d.dispose();
    }

    private void drawIcon(JComponent c, Graphics2D g2d, ActionOperation action, int stroke) {
      g2d.setStroke(new BasicStroke(stroke));
      
      int iconWidth = (int) (Math.min(c.getWidth(), c.getHeight()) * 0.75);
      
      Point lineEnd = new Point(c.getWidth() / 2, c.getHeight() / 2);
      if(action == ActionOperation.CLOSE) {
        lineEnd = new Point((c.getWidth() + iconWidth) / 2, c.getHeight() / 2);
      }
      g2d.drawLine((c.getWidth() - iconWidth) / 2, c.getHeight() / 2, lineEnd.x, lineEnd.y);
      if(action == ActionOperation.CLOSE) {
        lineEnd = new Point(c.getWidth() / 2, (c.getHeight() + iconWidth) / 2);
      }      
      g2d.drawLine(c.getWidth() / 2, (c.getHeight() - iconWidth) / 2, lineEnd.x, lineEnd.y);
    }
  }
